Global class CwsRestClient
{   
   /*
   Class Name:  CwsRestClient
   Date: 14-Jun-2011
   Requirement/Project Name: CXG
   Requirement/Project Description: Pulling New Notes from CSS and Inserting back to Salesforce
   */
        WebService static void CSSNotesCall(String CustomerId){
        List<notes__c> notelst= New List<notes__c>();
        Account acct=[Select Id from Account where CSS_CustomerID__c=:CustomerId];        
        List<CssNote> notes = GetCssNotes(CustomerId, 'A', 40);
        for(CssNote note : notes){
            System.debug('Here is a note:');
            System.debug('Creation Date:' + note.CreationDate);
            System.debug('Type:' + note.NoteType);
            System.debug('Source:' + note.NoteSource);
            System.debug('Text:' + note.NoteText);
            notes__c n = new notes__c();
            n.Date__c=note.CreationDate;
            n.Note__c=note.NoteSource;
            if(n.Note__c=='C' ||n.Note__c=='CSO')
            {
              n.Note__c='CSO';
            } 
            if(n.Note__c=='F')
            {
              n.Note__c='Finance';
            }
            if(n.Note__c=='S')
            {
             n.Note__c='SFA';
            }
            if(n.Note__c=='B')
            {
             n.Note__c='Billing Disputes/Claims';
            }
            n.Notes__c=note.NoteText;
            n.Account__c=acct.Id;
            n.Note_Type__c=note.NoteType; 
            notelst.add(n);  
            }   
            Insert notelst;
            }         
        Public class CssNote{
        public string NoteText;
        public string NoteSource;
        public string NoteType;
        public DateTime CreationDate;
    }    
    public Static  List<CssNote> GetCssNotes(string customerId, string noteSource, integer maxRecords)
    {
        AppEndPoints__c myCS = AppEndPoints__c.getInstance();
        String Val = myCS.CXGEndURL__c;
        //string envSpecificEndpoint = 'https://test-sfdc.atti.com/Salesforce/';
        string envSpecificEndpoint = Val;
        List<CssNote> notes = new List<CssNote>();
        CssNote note;
        string endpoint = envSpecificEndpoint + 'CwsCustomer.svc/GetContactHistoryAndBillingNotes?customerId=' + customerId + '&noteSource='+ noteSource +'&maxRecords=' + maxRecords;
        
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint(endpoint );
        req.setTimeout(10000);
        
        Http http = new Http();
        HttpResponse res = http.send(req);
        dom.Document doc = res.getBodyDocument();
        string ns = doc.getRootElement().getNamespace();
        string temp;
                
        for(dom.XmlNode node : doc.getRootElement().getChildElements()) {            
            note = new CssNote();
            note.NoteText = node.getChildElement('NoteText', ns).getText();
            note.NoteType = node.getChildElement('NoteType', ns).getText();
            note.NoteSource = node.getChildElement('NoteSource', ns).getText();
            temp = node.getChildElement('CreationDate', ns).getText();            
            note.CreationDate = ParseDate(temp);
            notes.add(note);
           }    
         return notes;
        }

    private static DateTime ParseDate(string strDate)
    {
        integer year = integer.valueof(strDate.substring(0,4));
        integer month  = integer.valueof(strDate.substring(5,7));
        integer day  = integer.valueof(strDate.substring(8,10));
        integer hour= integer.valueof(strDate.substring(11,13));
        integer minute=integer.valueof(strDate.substring(14,16));
        integer second=integer.valueof(strDate.substring(17,19));
        return datetime.newinstance(year,month,day,hour,minute,second);
    }   
 }